// pages/studyRecord/sentencePattern/sentencePattern.js
//获取应用实例
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');
var list = [
  {
    "id": 1,
    "detail": "at home",
    "translate": "在家",
    "audio": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
  },
  {
    "id": 2,
    "detail": "I never get up early on Sundays. Today is Sunday.I think he is a good girl Where there is a will,there is a way",
    "translate": "有志者事竟成，哈哈哈哈哈，有志者事竟成，我周末的时候一般都起的比较晚",
    "audio": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
  },
  {
    "id": 3,
    "detail": "I never get up early on Sundays.",
    "translate": "回家",
    "audio": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
  }
];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    innerAudioContext: new Object(),
    sentenceList:[],
    id: 1,   //万能句型分组id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    //接受传过来的分组id
    that.setData({
      id: options.id
    })
    console.log("传过来的句子id",that.data.id);
    // 获取句子详情数据
    that.getData();
    //音频插件处理
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    


    let temparr = that.data.sentenceList;
    for (let i = 0; i < temparr.length; i++) {
      temparr[i].status = 0;
      temparr[i].click = 0;
      temparr[i].fontw = 'normal'
    }
    that.setData({
      sentenceList: temparr
    })
  },

  /**
   * 请求获取万能句型列表
   */
  getData: function() {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getsentencelist({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      id: that.data.id,
    }).then((res) => {
      // status 0 英文
       // let temparr = that.data.sentenceList;
      let temparr = res.data;
      for (let i = 0; i < temparr.length; i++) {
        temparr[i].status = 0;
        temparr[i].click = 0;
        temparr[i].fontw = 'normal'
      }
      that.setData({
        sentenceList: temparr
      })

    });

  },

  // 点击句子
  clickWorld: function(e) {
    let tmp = new Object;
    tmp = this.data.sentenceList;
    let idx = e.currentTarget.dataset.idx;
    this.data.innerAudioContext.src = e.currentTarget.dataset.audiosrc;
    this.data.innerAudioContext.play();

    if (tmp[idx].click == 0) {
      tmp[idx].fontw = 600;
    } else if (tmp[idx].click == 1) {
      tmp[idx].bgcolor = "#D6FFE7";
      tmp[idx].status = 1;
    } else if (tmp[idx].click == 2){
      if (tmp[idx].status == 1) {
        tmp[idx].status = 0;
      } else {
        tmp[idx].status =1;
      }
     
    }
    if (tmp[idx].click < 2) {
      tmp[idx].click = parseInt(tmp[idx].click) + 1;
    }
    this.setData({
      sentenceList: tmp,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 点击句子
   */
  clickSentence: function(e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})